﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;


using Meqs.Entity;
using Meqs.Logic.Interface;

using Castle.Services.Transaction;
using Castle.Facilities.IBatisNetIntegration;
using log4net;

namespace Meqs.Logic.Implement
{
    public class ExamsEducationLogic : CommonLogic<ExamsEducation, int>, IExamsEducationLogic
    {
        /// <summary>
        /// 获取某次考试的学生信息
        /// </summary>
        /// <param name="ht"></param>
        /// <returns></returns>
        public IList<StudentInfo> FindStudent(Hashtable ht)
        {
            try
            {
                string statementName = "ExamsEducation.FindStu";
                return ExecuteQueryForList<StudentInfo>(statementName, ht);
            }
            catch (Exception ex)
            {
                ILog _log = LogManager.GetLogger("DatabaseOp");
                _log.Error("查询全部对象出错：" + ex.Message, ex);
                throw;
            }
        }
        /// <summary>
        /// 查找学生参加的考试
        /// </summary>
        /// <param name="ht"></param>
        /// <returns></returns>
        public IList<ExamsEducation> FindStudentExam(Hashtable ht)
        {
            try
            {
                string statementName = "ExamsEducation.FindStuExam";
                return ExecuteQueryForList<ExamsEducation>(statementName, ht);
            }
            catch (Exception ex)
            {
                ILog _log = LogManager.GetLogger("DatabaseOp");
                _log.Error("查询全部对象出错：" + ex.Message, ex);
                throw;
            }
            return null;
        }

        #region Implementation of ICommonLogic<ExamsEducation, int>

        /// <sammury>
        /// 创建对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Create(ExamsEducation entity)
        {
            return CreateObject(entity);
        }

        /// <sammury>
        /// 更新对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Update(ExamsEducation entity)
        {
            return UpdateObject(entity);
        }

        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="entityId"></param>
        /// <returns></returns>
        public int Delete(int entityId)
        {
            return DeleteObject(entityId);
        }

        /// <summary>
        /// 删除所有对象
        /// </summary>
        /// <returns></returns>
        public int DeleteAll()
        {
            return DeleteAllObject();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entityId"></param>
        /// <returns></returns>
        public ExamsEducation FindById(int entityId)
        {
            return FindObjectById(entityId);
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <returns></returns>
        public int FindCount()
        {
            return FindObjectByCount();
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        public IList<ExamsEducation> FindAll()
        {
            return FindAllObject();
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <returns></returns>
        public int FindCount(Hashtable table)
        {
            return FindObjectByCount(table);
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        public IList<ExamsEducation> FindAll(Hashtable table)
        {
            return FindObject(table);
        }

        #endregion

        private readonly IExamSubjecyEducationLogic eduExamSubjectLogic;
        public ExamsEducationLogic(IExamSubjecyEducationLogic _eduExamSubjectLogic)
        {
            eduExamSubjectLogic = _eduExamSubjectLogic; 

        }


        /// <summary>
        /// 创建局级考试，以及考试对应科目信息
        /// </summary>
        /// <param name="eduExam"></param>
        /// <param name="listSubject"></param>
        /// <returns></returns>
        [Transaction(TransactionMode.Requires)]
        public int InsertEduExam(ExamsEducation eduExam, List<CauseInfo> listSubject)
        {
            int exameId = Create(eduExam);
            if (exameId != -1)
            {
                foreach (CauseInfo item in listSubject)
                {
                    ExamSubjecyEducation entity = new ExamSubjecyEducation();
                    entity.ExamID = exameId;
                    entity.SubjectID = item.RID;
                    eduExamSubjectLogic.Create(entity);
                }
            }
            return exameId;
        }


        /// <summary>
        /// 删除局级考试及其科目信息
        /// </summary>
        /// <param name="examId"></param>
        /// <returns></returns>
        [Transaction(TransactionMode.Requires)]
        public int DelEduExam(int examId)
        {
            DeleteCustomer("ExamSubjecyEducation.DeleteByExamId", examId);
            DeleteCustomer("ExamsEducation.DeleteById", examId);
            return 1;
        }

        /// <summary>
        /// 修改局级考试，及其科目信息
        /// </summary>
        /// <param name="eduExam"></param>
        /// <param name="listSubject"></param>
        /// <returns></returns>
        [Transaction(TransactionMode.Requires)]
        public int UpdateEudExam(ExamsEducation eduExam, List<CauseInfo> listSubject)
        {
            Update(eduExam);
            DeleteCustomer("ExamSubjecyEducation.DeleteByExamId",eduExam.RID);

            foreach (CauseInfo item in listSubject)
            {
                ExamSubjecyEducation entity = new ExamSubjecyEducation();
                entity.ExamID = eduExam.RID;
                entity.SubjectID = item.RID;
                eduExamSubjectLogic.Create(entity);
            }
            return 1;
        }


        /// <summary>
        /// 下发考试
        /// </summary>
        /// <param name="RID">考试id</param>
        /// <returns></returns>
        public int PublishedExam(int RID)
        {
            return UpdateCustomer("ExamsEducation.PublishedExam", RID);
        }


        /// <summary>
        /// 结束考试
        /// </summary>
        /// <param name="RID">考试id</param>
        /// <returns></returns>
        public int ClosedExam(int RID)
        {
            return UpdateCustomer("ExamsEducation.ClosedExam", RID);
        }


        /// <summary>
        /// 申报考试
        /// </summary>
        /// <param name="RID">考试id</param>
        /// <returns></returns>
        public int UpReportExam(int RID)
        {
            return UpdateCustomer("ExamsEducation.UpReportExam", RID);
        }

        /// <summary>
        /// 获取教育统考考试
        /// </summary>
        /// <param name="areaId">学区id</param>
        /// <param name="grades">年级段</param>
        /// <returns></returns>
        public IList<VExamsEducation> SelectSchoolEduExam(int areaId, string grades,string schoolMark)
        {
            Hashtable table = new Hashtable();
            table.Add("AreaID", areaId);
            table.Add("SchoolMark", schoolMark);
            if (string.IsNullOrEmpty(grades))
                grades = "(0)";
            table.Add("grades", grades);
            return FindCustomer<VExamsEducation>("ExamsEducation.SelectSchoolEduExam", table);
        }

    }
}